package com.benefm.multipar.pad.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.benefm.multipar.pad.R;

public class CircleProgressBar extends View {
    private Paint backgroundPaint;
    private Paint foregroundPaint;
    private Paint textPaint;
    private Paint unitPaint;

    private float strokeWidth = 20f;
    private float progress = 0f;
    private int min = 0;
    private int max = 100;

    private int backgroundColor = Color.LTGRAY;
    private int foregroundColor = Color.BLUE;
    private int textColor = Color.BLACK;
    private float textSize = 40f;
    private float unitTextSize = 24f;

    private RectF rectF;
    private String progressText = "0";
    private String unitText = "ppm";

    public CircleProgressBar(Context context) {
        super(context);
        init();
    }

    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.CircleProgressBar, 0, 0);

        try {
            strokeWidth = typedArray.getDimension(R.styleable.CircleProgressBar_strokeWidth, strokeWidth);
            backgroundColor = typedArray.getColor(R.styleable.CircleProgressBar_backgroundColor, backgroundColor);
            foregroundColor = typedArray.getColor(R.styleable.CircleProgressBar_progressColor, foregroundColor);
            textColor = typedArray.getColor(R.styleable.CircleProgressBar_textColor, textColor);
            textSize = typedArray.getDimension(R.styleable.CircleProgressBar_textSize, textSize);
            max = typedArray.getInt(R.styleable.CircleProgressBar_max, max);
            min = typedArray.getInt(R.styleable.CircleProgressBar_min, min);
        } finally {
            typedArray.recycle();
        }

        init();
    }


    private void init() {
        // 背景圆环画笔
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(strokeWidth);
        backgroundPaint.setStrokeCap(Paint.Cap.ROUND);

        // 前景圆环画笔
        foregroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        foregroundPaint.setColor(foregroundColor);
        foregroundPaint.setStyle(Paint.Style.STROKE);
        foregroundPaint.setStrokeWidth(strokeWidth);
        foregroundPaint.setStrokeCap(Paint.Cap.ROUND);

        // 进度值文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // 单位文字画笔
        unitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        unitPaint.setColor(textColor);
        unitPaint.setTextSize(unitTextSize);
        unitPaint.setTextAlign(Paint.Align.CENTER);

        rectF = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 计算圆环的绘制区域
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        float radius = Math.min(centerX, centerY) - strokeWidth / 2f;

        rectF.set(centerX - radius, centerY - radius,
                centerX + radius, centerY + radius);

        // 绘制背景圆环
        canvas.drawArc(rectF, 0, 360, false, backgroundPaint);

        // 绘制前景圆环（进度部分）
        float angle = 360 * progress / (max - min);
        canvas.drawArc(rectF, -90, angle, false, foregroundPaint);

        // 更新进度文本
        progressText = String.valueOf((int)progress);

        // 计算文字垂直位置
        float textHeight = textPaint.descent() - textPaint.ascent();
        float unitHeight = unitPaint.descent() - unitPaint.ascent();
        float totalHeight = textHeight + unitHeight;

        // 绘制进度值（上方）
        float textY = centerY - totalHeight / 2 + textHeight;
        canvas.drawText(progressText, centerX, textY, textPaint);

        // 绘制单位（下方）
        float unitY = textY + unitHeight;
        canvas.drawText(unitText, centerX, unitY, unitPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 确保View是正方形的
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = Math.min(width, height);
        setMeasuredDimension(size, size);
    }

    // 设置进度
    public void setProgress(float progress) {
        this.progress = progress;
        if (this.progress > max) {
            this.progress = max;
        }
        if (this.progress < min) {
            this.progress = min;
        }
        invalidate(); // 重绘View
    }

    // 设置最大值
    public void setMax(int max) {
        this.max = max;
        invalidate();
    }

    // 设置最小值
    public void setMin(int min) {
        this.min = min;
        invalidate();
    }

    // 设置圆环宽度
    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        backgroundPaint.setStrokeWidth(strokeWidth);
        foregroundPaint.setStrokeWidth(strokeWidth);
        invalidate();
    }

    // 设置背景颜色
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        backgroundPaint.setColor(backgroundColor);
        invalidate();
    }

    // 设置前景颜色
    public void setForegroundColor(int foregroundColor) {
        this.foregroundColor = foregroundColor;
        foregroundPaint.setColor(foregroundColor);
        invalidate();
    }

    // 设置文字颜色
    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textPaint.setColor(textColor);
        unitPaint.setColor(textColor);
        invalidate();
    }

    // 设置进度值文字大小
    public void setTextSize(float textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        invalidate();
    }

    // 设置单位文字大小
    public void setUnitTextSize(float unitTextSize) {
        this.unitTextSize = unitTextSize;
        unitPaint.setTextSize(unitTextSize);
        invalidate();
    }

    // 设置单位文本
    public void setUnitText(String unitText) {
        this.unitText = unitText;
        invalidate();
    }
}
